/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.misena.praxispraktik.recursos;

import com.misena.praxispraktik.entity.Instructores;
import com.misena.praxispraktik.entity.Instructores_;
import com.misena.praxispraktik.entity.Personas;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.TypedQuery;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.ParameterExpression;
import javax.persistence.criteria.Root;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;

/**
 *
 * @author Avila
 */
@Stateless
@Path("/consulinstructoridper")
public class ConsulInstructorIdPer {
    
    Instructores instructor;
    EntityManagerFactory emf = null;
    EntityManager em = null;

    @GET
    @Produces("application/json")
    public Instructores consultar(@QueryParam("idPersonas") Integer idPersonas) {
        //public String consultar(@QueryParam("id") Integer id){
        //public String consultar(){
        try {
            emf = obtenerEntityManagerFactory();
            em = obtenerEntityManager(emf);
            
            Personas persona;
            persona = em.find(Personas.class, idPersonas);
            
            //CriteriaQuery<Usuarios> criteria = build.createQuery( Usuarios.class );
             CriteriaQuery criteria =em.getCriteriaBuilder().createQuery();
             Root<Instructores> instructoresRoot = criteria.from( Instructores.class );
             criteria.select( instructoresRoot );
             ParameterExpression<Personas> IdPersona = em.getCriteriaBuilder().parameter( Personas.class );
             criteria.where( em.getCriteriaBuilder().equal( instructoresRoot.get( Instructores_.idPersonas), IdPersona ));
             TypedQuery<Instructores> query = em.createQuery( criteria );
             query.setParameter( IdPersona, persona);
             instructor = query.getSingleResult();
             
            
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            em.close();
            emf.close();
        }
        System.out.println("Fin Consulta");

        //System.out.println(username.getIdUsuarios() + " " + username.getIdPersonas());

        return instructor;
    }
    
    
     public EntityManagerFactory obtenerEntityManagerFactory() {

        return Persistence.createEntityManagerFactory("PraxisPraktikPU");
    }

    public EntityManager obtenerEntityManager(EntityManagerFactory emf) {

        return emf.createEntityManager();
    }
    
    
}
